package com.example.classconnect.fragments;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.classconnect.R;
import com.example.classconnect.UpdateTable;
import com.example.classconnect.User;

public class FriendsFragment extends Fragment {

	//We need to find a way to pull these from the MainActivity
	private ArrayList<String> allEmailList;
	private ArrayList<String> friendsEmailList;
	private User currentUser;
	private ListView friends;
	private ListView all;
	
	
	
	public FriendsFragment() {
			
	};
	

	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		
        View rootView = inflater.inflate(R.layout.fragment_friends, container, false);
        currentUser = getArguments().getParcelable("CURRENTUSER");
        friendsEmailList = currentUser.getFriendsList();
        allEmailList = getArguments().getStringArrayList("ALLUSERS");
        allEmailList.remove(currentUser.getEmail());
        for(String email : friendsEmailList){
        	allEmailList.remove(email.substring(email.lastIndexOf(' ') + 1));
        }
        friends = (ListView) rootView.findViewById(R.id.classes);
        all = (ListView) rootView.findViewById(R.id.allUsers);
        ArrayAdapter<String> adapter
        
        = new ArrayAdapter<String>(getActivity(),
      
             android.R.layout.simple_list_item_1,
      
             friendsEmailList);

		friends.setAdapter(adapter);
		ArrayAdapter<String> adapter2
        
        = new ArrayAdapter<String>(getActivity(),
      
             android.R.layout.simple_list_item_1,
      
             allEmailList);

		all.setAdapter(adapter2);
		setHasOptionsMenu(true);
	    registerForContextMenu(all);
        return rootView;
        
    }
	


	@Override
	public boolean onContextItemSelected(MenuItem item) {
	    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    switch(item.getItemId()) {
	    case R.id.item1:
	    	currentUser.addFriend(allEmailList.get((int)info.id));
 			Intent myIntent = new Intent(getActivity(), UpdateTable.class);
 			myIntent.putExtra("UPDATEUSER", currentUser);
 			getActivity().startActivity(myIntent);
	    	Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
 			emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { allEmailList.get((int)info.id) });
 			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Friend Request");
 			emailIntent.putExtra(Intent.EXTRA_TEXT, allEmailList.get((int)info.id) + " has added you as a friend on Class Connect!");
 			emailIntent.setType("plain/text");
 			startActivity(emailIntent);
 			getActivity().finish();
	        return true;
	    }
	    return super.onContextItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
	        ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);

	    getActivity().getMenuInflater().inflate(R.menu.context_menu, menu);
	}
	
}
